//
//  MyMoneyBageViewController.m
//  AllFamily
//
//  Created by chirui on 2018/6/6.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "MyMoneyBageViewController.h"
#import "TopUpBalanceVC.h"
#import "BankListViewController.h"
#import "IwantWithdrawViewController.h"
#import "WithdrawListViewController.h"
#import "PointTurnViewController.h"
#import "GetAndTopViewController.h"

@interface MyMoneyBageViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    NSDictionary * dataDic;
}

@end

@implementation MyMoneyBageViewController


-(void)viewWillAppear:(BOOL)animated{
    
    if (verifTokenAndEnter) {
        
          [self getBalanceRequest];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
    TitleView(@"我的钱包");
  
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightItemWithTitle:@"银行卡" target:self action:@selector(bankCard)];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_renter" target:self action:@selector(backBtnClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)bankCard{
    
    BankListViewController * bank = [[BankListViewController alloc]init];
    [self.navigationController pushViewController:bank animated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return  1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 180;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * cellId = @"cellId";
    
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId ];
    if (cell==nil) {
        
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }else {
        while ([cell.contentView.subviews lastObject] != nil) {
            
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
            
        }
    }
    NSArray * titleArray = @[@"充值",@"提现",@"积分转账",@"提现记录",@"收支明细"];
    NSArray * imageArray = @[@"icon_wallet_recharge",@"icon_wallet_reflect",@"icon_wallet_ta",@"icon_wallet_present_record",@"icon_wallet_present_bp"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    cell.textLabel.font =[UIFont systemFontOfSize:15];
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image = ImageName(imageArray[indexPath.row]);
        cell.textLabel.text = titleArray[indexPath.row];
   
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * heardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 180)];
    heardView.backgroundColor = [UIColor whiteColor];
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 180)];
    imageView.image = ImageName(@"img_wallet_bg");
    
    UIView * line = [[UIView alloc]initWithFrame:CGRectMake(WIDTH/2-1, 60, 0.5, 50)];
    line.backgroundColor = [UIColor lightTextColor];
    [imageView addSubview:line];
    
  
    [self creatLabelFrame:CGRectMake(0, 60, WIDTH/2-1, 20) andTitle:@"当前账户余额(元)" andFont:13 onView:imageView];
    
    [self creatLabelFrame:CGRectMake(WIDTH/2, 60, WIDTH/2, 20) andTitle:@"当前消费积分" andFont:13 onView:imageView];
    if (dataDic) {
        [self creatLabelFrame:CGRectMake(0, 80, WIDTH/2-1, 30) andTitle:dataDic[@"balance"] andFont:22 onView:imageView];
        [self creatLabelFrame:CGRectMake(WIDTH/2, 80, WIDTH/2, 30) andTitle:dataDic[@"score"] andFont:22 onView:imageView];
    }
    
    
    [heardView addSubview:imageView];
    
    
    return heardView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row==0) {
        
        TopUpBalanceVC * top = [[TopUpBalanceVC alloc]init];
        [self.navigationController pushViewController:top animated:YES];
    }else if (indexPath.row==1){
        
        IwantWithdrawViewController * want = [[IwantWithdrawViewController alloc]init];
        want.money = dataDic[@"balance"];
        [self.navigationController pushViewController:want animated:YES];
    }else if (indexPath.row==2){
        PointTurnViewController * point = [[PointTurnViewController alloc]init];
        point.pointStr = dataDic[@"score"];
        [self.navigationController pushViewController:point animated:YES];
        
    }else if (indexPath.row==3){
        
        WithdrawListViewController *  List = [[WithdrawListViewController alloc]init];
        [self.navigationController pushViewController:List animated:YES];
    }else {
        
        GetAndTopViewController *  get = [[GetAndTopViewController alloc]init];
        [self.navigationController pushViewController:get animated:YES];
    }
    
}

-(void)creatLabelFrame:(CGRect)rec andTitle:(NSString *)title andFont:(CGFloat)flot onView:(UIView *)view{
    
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:rec];
    titleLabel.text = title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:flot];
    [view addSubview:titleLabel];
}

-(void)getBalanceRequest{
    
    NSDictionary * dic = @{@"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token")
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr( get_balanceMoney_api) andparameters:dic Success:^(id responseObject) {
        if (verifTokenAndEnter) {
            
            dataDic = responseObject[@"datas"];
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        [_tableView reloadData];
    } failed:^(NSError *error) {
        SHuiMBhudTextNetError;
    }];
}

@end
